//
//  GHPicCollectionCell.m
//  VideoPic
//
//  Created by GH on 16/3/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "GHPicCollectionCell.h"
#import "GHPicTableviewCell.h"
#import "NetworkingHandle.h"
#import "GHPicGroupModel.h"
#import "GHManyPic.h"
#import "GHPicComment.h"
#import "HomeViewController.h"

@interface GHPicCollectionCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)NSMutableArray *arrayData;
@property (nonatomic ,retain)NSString *strId;
@property (nonatomic ,retain)NSString *min_time;
@property (nonatomic ,retain)NSMutableArray *arrMin;

@end


@implementation GHPicCollectionCell


- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self data];
        [self createTableView];
    }
    return self;
}


- (void)data {
    if (self.arrayData == nil) {
        self.arrayData = [NSMutableArray array];
    }
    self.arrMin = [NSMutableArray array];


    NSString *url = @"http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-103&iid=3914532857&os_version=9.2.1&os_api=18&app_name=joke_essay&channel=App%20Store&device_platform=iphone&idfa=177896AB-D8CF-4150-99C2-8E562731F6F7&vid=ACE4A108-7577-4471-A722-91852E0C04E1&openudid=80dcf8bba6449077e78a4785ac7799f2aaee1a32&device_type=iPhone%205S&version_code=5.0.1&ac=WIFI&screen_width=640&device_id=12714942149&aid=7&city=辽宁省&content_type=-103&count=30&essence=1&latitude=38.88253453823842&longitude=121.5397050813396&message_cursor=0&min_time=1458522762&mpic=1";
    [NetworkingHandle handleWorkData:url AndBody:nil value:nil handleBlock:^(id result) {
        NSLog(@"%@",url);
        
        NSDictionary *dicData = [result objectForKey:@"data"];
        self.min_time = [dicData objectForKey:@"min_time"];
        NSArray *arrCon = [dicData objectForKey:@"data"];

         for (NSDictionary *dicCon in arrCon) {
             if ([[dicCon objectForKey:@"type"] isEqualToNumber:@5]) {
                 NSLog(@"广告");
             }else {
             
                 NSDictionary *dicGroup = [dicCon objectForKey:@"group"];
                 GHPicGroupModel *model = [[GHPicGroupModel alloc] init];
                 if (dicGroup) {
                     [self.arrMin addObject:[dicGroup objectForKey:@"id"]];
                 }
                 [model setValuesForKeysWithDictionary:dicGroup];
                 
                 [self.arrayData addObject:model];
                 
#pragma mark type
               model.media_type = [dicGroup objectForKey:@"media_type"];

#pragma mark pic
                 if ([[dicGroup objectForKey:@"media_type"]isEqual:@1]) {
                     NSDictionary *pic = [dicGroup objectForKey:@"large_image"];
                     [model setValuesForKeysWithDictionary:pic];
                     NSArray *list =[pic objectForKey:@"url_list"];
//                     for (NSDictionary *dicPic in list) {
//                         model.picUrl = [dicPic objectForKey:@"url"];
//                     }
                     NSDictionary *dicPic = list[0];
                model.picUrl = [dicPic objectForKey:@"url"];

                 }
                 
                 
#pragma mark gif
                 if ([[dicGroup objectForKey:@"media_type"]isEqual:@2]) {
                     
                 
                 //gif
                 NSDictionary *gif = [dicGroup objectForKey:@"large_image"];
                 [model setValuesForKeysWithDictionary:gif];
                 
#pragma  mark gif Url
                 NSArray *list = [gif objectForKey:@"url_list"];
//                 for (NSDictionary *dicUrl in list) {
//               model.gifUrl = [dicUrl objectForKey:@"url"];
//                 }
                     NSDictionary *dicUrl = list[0];
                     model.gifUrl = [dicUrl objectForKey:@"url"];
                     
                     
                     
                 }
#pragma mark large image
                 if ([[dicGroup objectForKey:@"media_type"]isEqual:@4]) {
                     model.arrPic = [NSMutableArray array];
                     
                     NSArray *picLarge_image_list = [dicGroup objectForKey:@"large_image_list"];
                     for (NSDictionary *dicLarge in picLarge_image_list) {
                         [model setValuesForKeysWithDictionary:dicLarge];
                         model.width_large = [dicLarge objectForKey:@"width"];
                         model.height_large = [dicLarge objectForKey:@"height"];
                         [model.arrPic addObject:[dicLarge objectForKey:@"url"]];
                     }
                 }
                 
                 
#pragma user
                 //user
                 NSDictionary *dicUser = [dicGroup objectForKey:@"user"];
                 
                 [model setValuesForKeysWithDictionary:dicUser];
                 
             }
         }
        [self.tableview reloadData];
    }];



}



- (void)createTableView {
    self.tableview = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableview];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.tableview registerClass:[GHPicTableviewCell class] forCellReuseIdentifier:@"pool"];
    [self.tableview registerClass:[GHManyPic class] forCellReuseIdentifier:@"pool1"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GHPicGroupModel *model = [self.arrayData objectAtIndex:indexPath.row];
    if ([model.media_type isEqual:@4]) {
        GHManyPic *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        [cell prepareForReuse];
        [cell passModelWith:model];
        return cell;
    }else {
    
    GHPicTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    [cell passModelWith:model];
    [cell prepareForReuse];
    return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GHPicGroupModel *model = [self.arrayData objectAtIndex:indexPath.row];

    if ([model.media_type isEqual:@4]) {

        return [GHManyPic heightForCell:model ];
    }else {
   return [GHPicTableviewCell heightForCellWith:model];
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GHPicGroupModel *model = [self.arrayData objectAtIndex:indexPath.row];

    GHPicComment *comment = [[GHPicComment alloc] init];
    
    comment.model = model;
    
    id result = [self nextResponder];
    while (![result isKindOfClass:[HomeViewController class]] && result != nil) {
        result = [result nextResponder];
    }
    
    HomeViewController *vc =  (HomeViewController *)result;
    
    [vc.navigationController pushViewController:comment animated:YES];
    
}


@end
